﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using Common;
using Models;

namespace BLL
{
    public class CommentService : BaseService
    {
        private string DefaultUserLogo = "imgs/Null.gif";

        public List<CommentInfo> GetCommentsListForAdmin(string searchString)
        {
            return (from c in db.Comments
                    where c.Deleted == false && c.sContent.Contains(searchString)
                    orderby c.Id descending
                    select BuildCommentInfo(c)).ToList();
        }

        private CommentInfo BuildCommentInfo(Comment c)
        {
            var user = UserService.GetUserById(db, c.PostUserId);

            var comment = new CommentInfo()
            {
                Id = c.Id,
                Content = c.sContent,
                CreateTime = c.CreatedOn,
                UserName = user.UserName,
                UserType = (UserType)user.UserType
            };

            return comment;
        }

        public void DeleteCommentById(int Id)
        {
            var comment = (from u in db.Comments
                        where u.Id == Id
                        select u).FirstOrDefault();

            if (comment != null)
            {
                comment.Deleted = true;
                db.SubmitChanges();
            }
        }

        private string GetUserName(int userId)
        {
            var q = from u in db.Users
                    where u.Id == userId
                    select u.UserName;

            var result = q.SingleOrDefault();
            if (result != null)
            {
                return result;
            }
            else
            {
                return string.Empty;
            }

        }

      

        public PagedList<CommentInfo> GetCommentsByObjId(int objId, ObjType objType, int pageIndex, int pageSize)
        {
            
            var query = from c in db.Comments
                        where c.ObjId == objId && c.ObjType == (int) objType
                        && c.Deleted==false
                        orderby c.CreatedOn descending

                        select new CommentInfo()
                            {
                                Content = c.sContent,
                                CreateTime = c.CreatedOn,
                                UserName = this.GetUserName(c.PostUserId),
                                UserLogo = this.GetUserLogo(c.PostUserId),
                                Video = this.GetVideo(c.ObjId, c.ObjType)
                            };

            pageIndex = pageIndex == 0 ? 0 : pageIndex - 1;

            var comments = new PagedList<CommentInfo>(query, pageIndex, pageSize); 
            return comments;

        }

        public PagedList<CommentInfo> GetNormalCommentsByObjId(int pageIndex, int pageSize)
        {
            var query = from c in db.Comments
                        join u in db.Users on c.PostUserId equals u.Id
                         where c.Deleted == false
                        select new CommentInfo()
                        {
                            Content = c.sContent,
                            CreateTime = c.CreatedOn,
                            UserName = this.GetUserName(c.PostUserId),
                            UserLogo = this.GetUserLogo(c.PostUserId),
                            Video = this.GetVideo(c.ObjId, c.ObjType)

                        };
            query = query.OrderByDescending(b => b.CreateTime);
            pageIndex = pageIndex == 0 ? 0 : pageIndex - 1;
            var comments= new PagedList<CommentInfo>(query, pageIndex, pageSize);
            return comments;

        }

        public PagedList<CommentInfo> GetVipCommentsByObjId( int pageIndex, int pageSize)
        {
            var query = from c in db.Comments
                        join u in db.Users on c.PostUserId equals  u.Id
                        where u.UserType == (int) UserType.Vip
                        && (c.ObjType == (int) ObjType.微力十足 || c.ObjType == (int)ObjType.市长带你游)
                        && c.Deleted==false

                        select new CommentInfo()
                        {
                            Content = c.sContent,
                            CreateTime = c.CreatedOn,
                            UserName = this.GetUserName(c.PostUserId),
                            UserLogo =  this.GetUserLogo(c.PostUserId),
                            Video = this.GetVideo(c.ObjId, c.ObjType)
                        };

            query = query.OrderByDescending(b => b.CreateTime);
            pageIndex = pageIndex == 0 ? 0 : pageIndex - 1;
            var comments = new PagedList<CommentInfo>(query, pageIndex, pageSize);
            return comments;


        }

        private CommentVideoInfo GetVideo(int objId, int objType)
        {
            if ((ObjType)objType == ObjType.微力十足)
            {
                var query = from video in db.VideoInfos
                            where video.ID == objId
                            select new CommentVideoInfo
                {
                    Link = string.Format("MerakDetail.aspx?id={0}", video.ID),
                    Title = video.Title,
                    Author = video.Author,
                    Category = "微力十足",
                    Image = "Upload/videoimage/"+video.Image,
                    Point = video.Point==null?0:video.Point.Value,
                };
                return query.SingleOrDefault();

            }
            else if ((ObjType)objType == ObjType.市长带你游)
            {
                var query = from video in db.DBMayors
                            where video.ID == objId
                            select new CommentVideoInfo
                            {
                                Link = string.Format("MayorDetail..aspx?id={0}", video.ID),
                                Title = video.Title,
                                Author = "",
                                Category = "市长带你游",
                                Image = "Upload/mayorImage/" + video.Image,
                                Point = 0,
                            };
                return query.SingleOrDefault();
            }
            else if ((ObjType)objType == ObjType.媒体中心新闻)
            {
                var query = from video in db.DbNewsInfos
                            where video.ID == objId
                            select new CommentVideoInfo
                            {
                                Title = video.Title,
                                Author = "",
                                Category = "媒体中心新闻",
                                Image = "Upload/videoimage/" + video.Image
                            };
                return query.SingleOrDefault();
            }
            else if ((ObjType)objType == ObjType.主题活动)
            {
                var query = from video in db.DbNewsInfos
                            where video.ID == objId
                            select new CommentVideoInfo
                            {
                                Title = video.Title,
                                Author = "",
                                Category = "主题活动",
                                Image = "Upload/videoimage/" + video.Image
                            };
                return query.SingleOrDefault();
            }
            return null;
        }

        private string GetUserLogo(int userId)
        {
            var q = from u in db.Users
                    where u.Id == userId
                    select u.UserLogo;

            var result = q.SingleOrDefault();
            if (result != null)
            {
                return ConfigurationManager.AppSettings["AppDomain"] + result;
            }
            else
            {
                return ConfigurationManager.AppSettings["AppDomain"] + this.DefaultUserLogo;
            }
        }

        public void PostComment(int postUserId, string postContent, int objId, ObjType objType)
        {
            var newComment = new Comment();
            newComment.CreatedOn = DateTime.Now;
            newComment.Deleted = false;
            newComment.ObjId = objId;
            newComment.ObjType = (int) objType;
            newComment.Point = 0;
            newComment.PostUserId = postUserId;
            newComment.sContent = postContent;

            db.Comments.InsertOnSubmit(newComment);
            db.SubmitChanges();



        }
    }
}
